package gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** LowerCentralView contiene quei componenti che aiutano
 *  l'utente a visualizzare il suo lavoro e ad individuare
 *  posizioni esatte sull'inquadratura del Display.
 * @author Daniele
 * @version 1.2 */
public class CentralView extends JPanel{

	private static final long serialVersionUID = 6495032468385991248L;

	/** Il layout utilizzato per disporre i vari componenti. */
	private GridBagLayout layout;
	/** I constraints associati al GridBagLayout. */
	private GridBagConstraints constraints;
	/** Il Display che rappresenta visivamente il progetto in corso. */
	private Display display;
	/** Il JScrollPane necessario per scorrere su una porzione del display. */
	private JScrollPane displayScroll;
	/** Il righello orizzontale. */
	private Ruler hRuler;
	/** Il righello verticale. */
	private Ruler vRuler;
	/** L'etichetta che mostra la coordinata x del puntatore sul Display. */
	private JLabel xLabel;
	/** L'etichetta che mostra la coordinata y del puntatore sul Display. */
	private JLabel yLabel;
	/** Il pannello che mostra lo Stratum in evidenza sul numero totale di Strata. */
	private StrataOverviewPanel strataOverviewPanel;
	
	public CentralView() {
		
		super();
		
		layout = new GridBagLayout();
		constraints = new GridBagConstraints();
		this.setLayout(layout);
		
		display = new Display();
		hRuler = new Ruler(0, 0);
		vRuler = new Ruler(1, 0);
		//ScrollCorner corner1 = new ScrollCorner();
		//ScrollCorner corner2 = new ScrollCorner();
		//ScrollCorner corner3 = new ScrollCorner();
		
		if(display.isMapNull()){
			vRuler.setPreferredWidth(200);
			hRuler.setPreferredHeight(200);
		} else {
			vRuler.setPreferredWidth(display.getMap().getWidth());
			hRuler.setPreferredHeight(display.getMap().getHeight());
		}
		
		displayScroll = new JScrollPane(this.display);
		//FIXME sono queste istruzioni che appesantiscono in modo ABNORME le prestazioni... da migliorare!!!!
		/*displayScroll.setColumnHeaderView(vRuler);
		displayScroll.setRowHeaderView(hRuler);
		displayScroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner1);
		displayScroll.setCorner(JScrollPane.LOWER_LEFT_CORNER, corner2);
		displayScroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corner3);*/
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 0;
		constraints.weighty = 15;
		constraints.insets.top = 10;
		constraints.insets.left = 0;
		constraints.insets.bottom = 10;
		constraints.insets.right = 0;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		layout.setConstraints(displayScroll, constraints);
		//TODO istruzione INCREDIBILMENTE pesante! da quel che ho capito � il displayScroll che appensantisce in modo assurdo il componente!
		this.add(this.displayScroll, constraints);
		
		xLabel = new JLabel("  x = 0");
		JScrollPane xScroll = new JScrollPane(xLabel);
		this.initializeJScrollPane(xScroll);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weightx = 1;
		constraints.weighty = 0.5;
		constraints.insets.top = 10;
		constraints.insets.left = 10;
		constraints.insets.bottom = 10;
		constraints.insets.right = 10;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		layout.setConstraints(xScroll, constraints);
		this.add(xScroll, constraints);
		
		strataOverviewPanel = new StrataOverviewPanel();
		JScrollPane strataScroll = new JScrollPane(strataOverviewPanel);
		this.initializeJScrollPane(strataScroll);
		constraints.gridx = 1;
		constraints.gridy = 1;
		constraints.weightx = 1;
		constraints.weighty = 0.5;
		constraints.insets.top = 10;
		constraints.insets.left = 10;
		constraints.insets.bottom = 10;
		constraints.insets.right = 10;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		layout.setConstraints(strataScroll, constraints);
		this.add(strataScroll, constraints);
		
		yLabel = new JLabel("  y = 0");
		JScrollPane yScroll = new JScrollPane(yLabel);
		this.initializeJScrollPane(yScroll);
		constraints.gridx = 2;
		constraints.gridy = 1;
		constraints.weightx = 1;
		constraints.weighty = 0.5;
		constraints.insets.top = 10;
		constraints.insets.left = 10;
		constraints.insets.bottom = 10;
		constraints.insets.right = 10;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		layout.setConstraints(yScroll, constraints);
		this.add(yScroll, constraints);
	}
	
	/** Esegue tutte le operazioni necessarie per inizializzare
	 *  i JScrollPane relativi ai pannelli descrittivi della CentralView. */
	public void initializeJScrollPane(JScrollPane scroll){
		scroll.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3, true));
	}
	
	/** Getter per il Display.
	 * @return the display. For further information visit {@link #display} */
	public Display getDisplay() {
		return display;
	}

	/** Setter per il Display.
	 * @param display the display to set */
	public void setDisplay(Display display) {
		this.display = display;
	}

	/** Getter per xLabel.
	 * @return the xLabel. For further information visit {@link #xLabel} */
	public JLabel getxLabel() {
		return xLabel;
	}

	/** Setter per xLabel.
	 * @param xLabel the xLabel to set */
	public void setxLabel(JLabel xLabel) {
		this.xLabel = xLabel;
	}

	/** Getter per yLabel.
	 * @return the yLabel. For further information visit {@link #yLabel} */
	public JLabel getyLabel() {
		return yLabel;
	}
	/**
	 * @return the strataOverviewPanel. For further information visit {@link #strataOverviewPanel}
	 */
	public StrataOverviewPanel getStrataOverviewPanel() {
		return strataOverviewPanel;
	}
	

	/**Getter per il righello orizzontale.
	 * @return the hRuler */
	public Ruler gethRuler() {
		return hRuler;
	}

	/** Getter per il righello verticale.
	 * @return the vRuler */
	public Ruler getvRuler() {
		return vRuler;
	}
}
